package me.zhengjie.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.kwService.GeneralExamService;
import me.zhengjie.kwService.SysUsersGeneralExamsService;
import me.zhengjie.mapper.UserMapper;
import me.zhengjie.pojo.dto.GeneralExamDto;
import me.zhengjie.pojo.entity.GeneralExam;
import me.zhengjie.pojo.entity.SysUsersGeneralExams;
import me.zhengjie.pojo.entity.User;
import me.zhengjie.util.BaseResponse;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

/**
 * @Description TODO
 * @Author Long
 * @Date 2021/8/12 14:51
 */
@RestController
@RequestMapping("/api/kwGeneralExam")
@Slf4j
@Api(tags = "总考试前端控制器")
public class GeneralExamController {


    @Autowired
    private GeneralExamService generalExamService;
    @Autowired
    private SysUsersGeneralExamsService sysUsersGeneralExamsService;
    @Autowired
    private UserMapper userMapper;

    @ApiOperation("分页查询总考试")
    @PostMapping("/findPage")
    public BaseResponse findPage(@RequestBody @Validated GeneralExamDto dto) {
        return BaseResponse.ok(generalExamService.findPage(dto));
    }

    @ApiOperation(value = "新增或修改总考试", notes = "有id则为修改，无id则为新增")
    @PostMapping("/saveOrUpdateOne")
    public BaseResponse saveOrUpdateOne(@RequestBody GeneralExam generalExam) {
        UserDetails currentUser = SecurityUtils.getCurrentUser();
        String username = currentUser.getUsername();
        /*根据当前登陆用户名获取登录用户的id*/
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", username);
        User user = userMapper.selectOne(userQueryWrapper);

        if (generalExam.getId() == null) {
            /*如果为新增，给中间表添加总考试和用户的关联关系*/
            Set<Long> userIds = generalExam.getUserIds();
            if (userIds == null || userIds.isEmpty()) return BaseResponse.ok();
            generalExamService.saveOrUpdate(generalExam);
            Integer generalExamId = generalExam.getId();
            /*给当前添加用户添加关联关系*/
            userIds.add(user.getUserId());
            for (Long userId : userIds) {
                SysUsersGeneralExams sysUsersGeneralExams = new SysUsersGeneralExams();
                sysUsersGeneralExams.setUserId(userId).setGeneralId(Long.valueOf(generalExamId));
                sysUsersGeneralExamsService.save(sysUsersGeneralExams);
            }
            return BaseResponse.ok();
        }
        generalExamService.saveOrUpdate(generalExam);
        return BaseResponse.ok();
    }

    @ApiOperation(value = "查询一个总考试", notes = "根据id查询")
    @GetMapping("/findOne")
    public BaseResponse findOne(@RequestParam("id") @NotNull(message = "总考试id不可为空") Integer id) {
        return BaseResponse.ok(generalExamService.findOne(id));
    }

    @ApiOperation("批量删除总考试")
    @DeleteMapping
    public BaseResponse deleteBatch(@RequestBody List<Integer> ids) {
        if (ids == null || ids.isEmpty()) return BaseResponse.ok();
        return generalExamService.deleteBatch(ids);
    }

    @ApiOperation("删除总考试下的所有考生")
    @DeleteMapping("/deleteAllKwStudent")
    @ApiImplicitParam(value = "总考试id", name = "ids", required = true)
    public BaseResponse deleteAllKwStudent(@RequestBody List<Long> ids) {
        return generalExamService.deleteAllKwStudent(ids);
    }

    @ApiOperation("查询所有考试")
    @GetMapping
    public BaseResponse findAll(){
        return generalExamService.findAll();
    }


}
